import * as api from '../../api/articles';

// model层
export default {
    namespaced: true, // 模块必须有这句话
    state: {
        detail: {},
        commentList: [],
        subCommentList: [],
    },
    mutations: {
        // 设置更改state的方法
        SET_DETAIL (state, newValue) {
            state.detail = newValue;
        },
        SET_COMMENT_LIST (state, newValue) {
            state.commentList = newValue;
        },
        SET_SUB_COMMENT_LIST (state, newValue) {
            state.subCommentList = newValue;
        },
    },
    actions: {
        // 发送异步请求（给页面设置state的机会）
        async updateArticleDetail (context, id) {
            const res = await api.getArticleDetail(id);
            context.commit('SET_DETAIL', res.data.data);
        },
        async updateCommentList (context, params) {
            const res = await api.getCommentList(params);
            context.commit(
                params.type === 'a'
                    ? 'SET_COMMENT_LIST'
                    : 'SET_SUB_COMMENT_LIST',
                res.data.data.results,
            );
        },
    },
}
